import { result } from "lodash";

/* 获取用户设备或者系统 */
export function getUserAgent() {
    const userAgent = window.navigator.userAgent;
    let os = "Unknown OS";
    let browser = "Unknown Browser";

    // 操作系统检测
    if (userAgent.includes("Windows NT 10.0")) os = "Windows 10";
    // 添加其他 Windows 版本的检测逻辑...

    // 浏览器检测
    if (userAgent.includes("Edg/")) {
        const match = userAgent.match(/Edg\/(\d+\.\d+)/);
        browser = match ? `Edge ${match[1]}` : "Edge";
    } else if (userAgent.includes("Chrome")) {
        const match = userAgent.match(/Chrome\/(\d+\.\d+)/);
        browser = match ? `Chrome ${match[1]}` : "Chrome";
    } else if (userAgent.includes("Safari")) {
        browser = "Safari";
    } else if (userAgent.includes("Firefox")) {
        browser = "Firefox";
    } else if (userAgent.includes("Opera") || userAgent.includes("OPR")) {
        browser = "Opera";
    }
    return `${os}/${browser}`;
}

/* 获取用户ip */
export async function getUserIp() {
    return new Promise((resolve) => {
        fetch('https://api.ipify.org?format=json')
            .then(response => response.json())
            .then(data => resolve(data.ip))
            .catch(error => {
                console.error('获取用户ip错误Error fetching IP:', error)
                resolve("未知")
            });
    })
}

/* 获取地区 */
export function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } else {
        console.log("[兼容性]Geolocation is not supported by this browser.");
    }
}

function showPosition(position: any) {
    let result = `Latitude:${position.coords.latitude}, Longitude:${position.coords.longitude}`
    console.log(result);
    return result
}